package it.cums.ui;

import it.cums.ui.localization.Messages;

import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	// Actions - important to allocate these only in makeActions, and then use
	// them
	// in the fill methods. This ensures that the actions aren't recreated
	// when fillActionBars is called with FILL_PROXY.
	private IContributionItem fOpenWindowsItem;
    private IContributionItem fShowViewMenu;
    
    //private SearchAction searchAction;
	
    
	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	// Creates the actions and registers them.
	// Registering is needed to ensure that key bindings work.
	// The corresponding commands keybindings are defined in the plugin.xml
	// file.
	// Registering also provides automatic disposal of the actions when
	// the window is closed.
	protected void makeActions(final IWorkbenchWindow window) {
		/* Menu: File */
		register(ActionFactory.CLOSE.create(window));
		getAction(ActionFactory.CLOSE.getId()).setText(Messages.ApplicationActionBarAdvisor_0);
		
	    register(ActionFactory.CLOSE_ALL.create(window));
	    getAction(ActionFactory.CLOSE_ALL.getId()).setText(Messages.ApplicationActionBarAdvisor_1);
	    
	    register(ActionFactory.SAVE.create(window));
	    getAction(ActionFactory.SAVE.getId()).setText(Messages.ApplicationActionBarAdvisor_2);
	    getAction(ActionFactory.SAVE.getId()).setToolTipText(Messages.ApplicationActionBarAdvisor_2);
		
	    register(ActionFactory.SAVE_ALL.create(window));
	    getAction(ActionFactory.SAVE_ALL.getId()).setText(Messages.ApplicationActionBarAdvisor_3);
	    
	    register(ActionFactory.PRINT.create(window)); 
	    getAction(ActionFactory.PRINT.getId()).setText(Messages.ApplicationActionBarAdvisor_4);
	    getAction(ActionFactory.PRINT.getId()).setToolTipText(Messages.ApplicationActionBarAdvisor_4);
	    
	    register(ActionFactory.QUIT.create(window));
	    getAction(ActionFactory.QUIT.getId()).setText(Messages.ApplicationActionBarAdvisor_5);
	    
	    /* Menu: Window */
	    register(ActionFactory.OPEN_NEW_WINDOW.create(window));
	    getAction(ActionFactory.OPEN_NEW_WINDOW.getId()).setText(Messages.ApplicationActionBarAdvisor_6);
		
	    fOpenWindowsItem = ContributionItemFactory.OPEN_WINDOWS.create(window);
	    fShowViewMenu = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
	    
	    register(ActionFactory.PREFERENCES.create(window));
	    getAction(ActionFactory.PREFERENCES.getId()).setText(Messages.ApplicationActionBarAdvisor_7);
	    
	    /* Menu: Help */
	    register(ActionFactory.ABOUT.create(window));
	    getAction(ActionFactory.ABOUT.getId()).setText(Messages.ApplicationActionBarAdvisor_8);
	    
        register(ActionFactory.HELP_CONTENTS.create(window));
        getAction(ActionFactory.HELP_CONTENTS.getId()).setText(Messages.ApplicationActionBarAdvisor_9);
        
        /* CoolBar: Contextual Menu */
        register(ActionFactory.LOCK_TOOL_BAR.create(window));
        getAction(ActionFactory.LOCK_TOOL_BAR.getId()).setText(Messages.ApplicationActionBarAdvisor_10);
	}

	protected void fillMenuBar(IMenuManager menuBar) {
		/* File Menu */
	    createFileMenu(menuBar);
	    
	    /* Search Menu */
	    createSearchMenu(menuBar);
		
		/* Window Menu */
	    createWindowMenu(menuBar);
		
	    /* Menu: Help */
	    createHelpMenu(menuBar);
	}
	
	/* Menu: File */
	private void createFileMenu(IMenuManager menuBar) {
	    MenuManager fileMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_11, IWorkbenchActionConstants.M_FILE);
	    menuBar.add(fileMenu);

	    fileMenu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
	    fileMenu.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
	    fileMenu.add(new Separator());
	    
	    fileMenu.add(getAction(ActionFactory.CLOSE.getId()));
	    fileMenu.add(getAction(ActionFactory.CLOSE_ALL.getId()));
	    fileMenu.add(new GroupMarker(IWorkbenchActionConstants.CLOSE_EXT));
	    fileMenu.add(new Separator());
	    
	    fileMenu.add(getAction(ActionFactory.SAVE.getId()));
	    fileMenu.add(getAction(ActionFactory.SAVE_ALL.getId()));
	    fileMenu.add(new Separator());

	    fileMenu.add(getAction(ActionFactory.PRINT.getId()));

	    fileMenu.add(new Separator());
	    fileMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

	    fileMenu.add(new Separator());
	    fileMenu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
	    fileMenu.add(new Separator());

	    fileMenu.add(getAction(ActionFactory.QUIT.getId()));
	}
	
	/* Menu: Search */
	private void createSearchMenu(IMenuManager menuBar) {
	    MenuManager searchMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_12, IWorkbenchActionConstants.MB_ADDITIONS);
	    menuBar.add(searchMenu);
	}
	
	/* Menu: Window */
	private void createWindowMenu(IMenuManager menuBar) {
	    MenuManager windowMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_13, IWorkbenchActionConstants.M_WINDOW);
	    menuBar.add(windowMenu);

	    windowMenu.add(getAction(ActionFactory.OPEN_NEW_WINDOW.getId()));
	    windowMenu.add(new Separator());

	    MenuManager showViewMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_14);
	    windowMenu.add(showViewMenu);
	    showViewMenu.add(fShowViewMenu);
	    
	    windowMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

	    windowMenu.add(fOpenWindowsItem);
	    
	    windowMenu.add(new Separator());
	    windowMenu.add(getAction(ActionFactory.PREFERENCES.getId()));
	}
	
	 /* Menu: Help */
	  private void createHelpMenu(IMenuManager menuBar) {
	    MenuManager helpMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_15, IWorkbenchActionConstants.M_HELP);
	    menuBar.add(helpMenu);
	    
	    helpMenu.add(getAction(ActionFactory.HELP_CONTENTS.getId()));

	    helpMenu.add(new Separator());

	    helpMenu.add(getAction(ActionFactory.ABOUT.getId()));
	  }
	
	
	protected void fillCoolBar(ICoolBarManager coolBar) {
		/* CoolBar Context Menu */
	    MenuManager coolBarContextMenuManager = new MenuManager(null, "it.cums.ui.CoolBarContextMenu");  //$NON-NLS-1$
	    coolBar.setContextMenuManager(coolBarContextMenuManager);
	    coolBarContextMenuManager.add(getAction(ActionFactory.LOCK_TOOL_BAR.getId()));
	    coolBarContextMenuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
	   
	    IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
	    coolBar.add(new ToolBarContributionItem(toolbar, "main")); //$NON-NLS-1$
	    toolbar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
	    toolbar.add(getAction(ActionFactory.SAVE.getId()));
	    toolbar.add(getAction(ActionFactory.PRINT.getId()));
	}
	
}
